/**
 * 主界面Tab区
 */
(function($){
	 var panelsContainer = null;
	 var tabsContainer = null;
	 $.fn.simpleTab = function(options, param){
			if (typeof options == 'string') {
				switch(options) {
				   case 'add':
						return addTab(param);
					case 'close':
						return closeTab(param);
					case 'select':
						return selectTab(param);
					case 'exists':
						return exists(param);
					case 'selected':
						return selected();
					case 'getTabPanelById':
					   return getTabPanelById();
				   default:
				      return;
				}
			}
		   tabsContainer = this;
		   panelsContainer = $("#ContentPannel");
		   
		  
		   tabsContainer.on('click','li > a.tab',function(){
			   selectTab($(this).attr('index'));
		   });
		 
		   tabsContainer.on('click','li > a.tab_close',function(){
		        closeTab($(this).attr('index'));
		   });
		  
	 };
	
	function addTab(param){
		 $('#overlay_startmenu').hide();
		 $('#start_menu_panel').hide(); 
		 $('#start_menu_panel .nicescroll-rails').show();
		 $('.nicescroll-rails').hide();
		 
		 if(!param.id) return;
		 if(exists(param.id)){
		     selectTab(param.id);
		     return;
		 }
		 //Global.blockUI();
		 $.ajax({
	         type: "GET",
	         cache: false,
	         url: param.url,
	         dataType: "html",
	         headers: {
	             decorator: "body"
	         },
	         success: function(f) {	        	
	        	 createTabPannel(f);
	        	 //Global.unblockUI();
	         }
		 });     
		 
		 createTabPannel = function(content){
			 tabsContainer.find('li').removeClass('selected');
			 var html="<li id=\"tabs_" + param.id + "\" class='selected' index=\"" + param.id + "\"><span><a class=\"tab\"><img src='"+WEB_ROOT+"/assets/images/icon16/" + param.icon + "' width='16' height='16'>" + param.title + "</a></span>"
			 if(param.closable){
				 html += "<a class=\"tab_close\" index=\"" + param.id + "\" title=\"关闭当前窗口\"></a>";
			 }
			 html += '</li>';
			 $(html).appendTo(tabsContainer);
			 $('<div id="tabs_' + param.id + '_panel"   style="width:100%" >' + content + '</div>').appendTo(panelsContainer);
			 Page.init(panelsContainer);
			 //panelsContainer.append("<iframe id=\"tabs_iframe_" +  param.id + "\" name=\"tabs_iframe_" +  param.id + "\" height=\"100%\" width=\"100%\" src=\"" +  param.url + "\" frameBorder=\"0\"></iframe>");

			 
			

			 
		 }
	}
	function selectTab(id){
		 if(!exists(id)) return;
		    tabsContainer.find('li').removeClass('selected');
	       // ContentPannel.find('iframe').hide();//改
	        tabsContainer.find('#tabs_' + id).addClass('selected');
	        //top.document.getElementById("tabs_iframe_" + id).style.display = 'block';//改
	}
	function closeTab(id){
		   var iframe = window.frames['tabs_'+id+'_iframe'];
		   if(iframe && typeof(iframe.onclose) == 'function')
		   if(!iframe.onclose()) return;
		   
		   var nextTab = $('#tabs_'+id, tabsContainer).next();
		   
		   $('#tabs_'+id, tabsContainer).remove();
		   $('#tabs_'+id+'_panel', panelsContainer).remove();
		   
		   
		   if(!nextTab.is('li'))
		      nextTab = $('div', tabsContainer).last();
		   var nextId = nextTab.attr('id');

		   if(nextId){
		      selectTab(nextId.substr(5));
		   }
	   }
	function exists(id){
		 return $('#tabs_'+id, tabsContainer).length > 0;
	}
	function getTabPanelById(id){
	       return $('#tabs_'+id+'_panel');
	}
	
	 
})(jQuery);